<?php
/**
 *PageStructure Controller
 *
 *@author S.Feil, T.Ziegler, M.El-Hindi <goatymail@googlemail.com>
 *27 May 2012
 *@copyright Copyright (c) 2012, S.Feil, T.Ziegler, M.El-Hindi
 *@package cms
 *
 */
/**
 * Class PageStructure Controller
 * manipulates page structure/menu
 *@package controller
 */
class PageStructure_Controller extends Controller{
    /**
     * Language in wich to show page
     * @var integer
     */
    private $iLang = '';

    /**
     * Shows error on page
     * @var string
     */
    private $sError="";

    /**
     * Reference to view of content section
     * @var object
     */
    private $oContentView;

    /**
     * Reference to model that retrieves page data from db
     * @var object
     */
    private $oContentModel;

    /**
     * Reference to model that retrieves navigation elements from db
     * @var object
     */
    private $oNavigationModel;

    /**
     * Reference to view of navigation section
     * @var object
     */
    private $oNavigationView;

    /**
     * Reference to view of left navigation section in cms, its shows the entire pagestructure
     * @var object
     */
    private $oLeftNavigationView;
     
    
    public function __construct(){
        //Gets Viewmanger reference from parent constructor
        parent::__construct();

        //Loads Models and Views
        $this->oContentModel = Loader::loadModel('pageStructure');
        $this->oNavigationModel = Loader::loadModel('navigation');
        $this->oContentView = $this->oViewmanager->getView('pageStructure');
        $this->oNavigationView = $this->oViewmanager->getView('navigation');
        $this->oLeftNavigationView = $this->oViewmanager->getView('leftnavigation');

        //Checks language set in Cookie, take default language specified in config if not set
        if (isset($_COOKIE['language'])){
            $this->iLang = intval($_COOKIE['language']);
        }else{
            $this->iLang = DEFAULT_LANGUAGE;
        }
    }
     
    /**
     * Checks GET-Parameters forwarded from Router::route
     * @param Array $aArgs Arguments to check
     */
    public function checkArgs($aArgs){
        //Supplied argument is always a method
        $sMethod = array_shift($aArgs);

        //Content Page has been edited and must be saved
        if ($sMethod == 'saveContentPage'){
            $iLid = $this->iLang;
            //Checks which Content Page to save
            if($_GET['id']){
                $iPid = $_GET['id'];
                //Checks if post patameters were transfered correcty
                if (isset($_POST['name'])&&isset($_POST['content'])){
                    //Passes data to model and call model method to update data in database
                    $this->sError = $this->oContentModel->updateContentPage($iPid, $_POST['name'], $_POST['content'], $iLid);
                    //Redirects back to the page that was edited
                    header("Location: index.php?co=contentPage&id=$iPid");
                    //Prevents from undesired execution of further code
                    exit();
                }
            }

            //Page Structure has been edited (positions, levels etc)
        }elseif ($sMethod == 'savePageStructure'){
            //Retrives page structure from database to iterate through it
            //Only pages that are in the database can be updated
            $aPageStructure = $this->oContentModel->getPageStructure($this->iLang);
            foreach($aPageStructure as $aPage){
                $iPid = $aPage['id'];
                //Passes data to model / call model method to update data in database
                $this->oContentModel->updatePageStructure($this->iLang, $iPid, $_POST["parent$iPid"], $_POST["position$iPid"],  $_POST["visible$iPid"], $_POST["name$iPid"]);
            }
            //Redirects back to the pagestructure page
            header('Location: index.php?co=pageStructure');
            //Prevents from undesired execution of further code
            exit();

            //User deletes page
        }elseif ($sMethod == 'deletePage'){
            //Get page ID
            $iPid = array_shift($aArgs);
            //Checks if the page is a category
            $iIsCategory = $this->oContentModel->isCategory($iPid);
            //Calls model method to delete page in database
            $this->sError = $this->oContentModel->deletePage($iPid);
            //If deleted page was a cetegory redirect to product page
            if($iIsCategory){
                //Method was called from a product page
                $url = $_SERVER['HTTP_REFERER'];
                header('Location: '.$url);
                exit();
            }
        }

    }
/**
 * Executes controller methods
 * @param array different arguments
 */
    public function execute($aArgs){
        $this->checkArgs($aArgs);
        //Passes error to display it on page, if no error occured an empty string is passed
        $this->oContentView->assign('error', $this->sError);
        
        //Gets page data form database and forward it to views, the data is retrieved from the models
        $this->oContentView->assign('nestedPageStructure', $this->oContentModel->getNestedPageStructure('NULL', $this->iLang));
        $this->oContentView->assign('arrayPageStructure', $this->oContentModel->getPageStructure($this->iLang));
        $this->oNavigationView->assign('contentANDproductPages', $this->oNavigationModel->getPageStructure('NULL', $this->iLang, "(type = 'contentPage' OR type = 'product')"));
        $this->oNavigationView->assign('products', $this->oNavigationModel->getPagesByTyp('', $this->iLang, 'product'));
        $this->oNavigationView->assign('categories', $this->oNavigationModel->getPagesByTyp('', $this->iLang, 'category'));
        $this->oLeftNavigationView->assign('leftnavigation', $this->oNavigationModel->getPageStructure('NULL', $this->iLang));

        //Inserts data in the correct sections of the website layout
        //Pagestructure page has ID 0
        //CMS is in German, ID for german is 1
        $this->oViewmanager->setSection('Title', $this->oContentModel->getPageTitle('0', '1'));
        $this->oViewmanager->setSection('Content', $this->oContentView->display());
        $this->oViewmanager->setSection('LeftNavigation', $this->oLeftNavigationView->display());
        $this->oViewmanager->setSection('Navigation', $this->oNavigationView->display());
    }

    //Ajax function which updates page structure
    public function updatePageStructure($aPageStructure){
        //Gets array from sortable
        $aPageStructure = $aPageStructure['list'];
        $iCounter = 1;
        
        //The page id is stored in the key of the array the value is the parent id
        foreach($aPageStructure as $iPageID => $iParent){
            $aPageID = explode("'", $iPageID);
            $iPageID = intval($aPageID[1]);

            $aParent = explode("'", $iParent);
            $iParent = intval($aParent[1]);

            if($iParent == 0){
                $iParent = 'null';
            }
            
            //Calls updatePageStructure for every page
            $this->oContentModel->updatePageStructure($this->iLang, $iPageID, $iParent, $iCounter, '1');
            $iCounter++;
        }

    }


}
?>
